package com.alvis.exam.service.impl;

import com.alvis.exam.domain.*;
import com.alvis.exam.repository.TClassMapper;
import com.alvis.exam.service.ClassService;
import com.alvis.exam.service.impl.BaseServiceImpl;
import com.alvis.exam.viewmodel.admin.administration.ClassEditRequestVM;
import com.alvis.exam.viewmodel.admin.administration.ClassPageRequestVM;
import com.alvis.exam.viewmodel.admin.station.StationDictPageRequestVM;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class ClassServiceImpl extends BaseServiceImpl<TClass> implements ClassService {

    private TClassMapper mapper;

    @Autowired
    public ClassServiceImpl(TClassMapper mapper){
        super(mapper);
        this.mapper = mapper;
    }

    public List<TClass> getAll() {
        TClassExample example = new TClassExample();
        return mapper.selectByExample(example);
    }

    public List<TClass> querySelective(ClassPageRequestVM model) {
        TClassExample example = new TClassExample();
        if (StringUtil.isNotEmpty(model.getKeyWord())) {

            TClassExample.Criteria departmentCriteria = example.createCriteria();
            departmentCriteria.andDepartmentLike("%" + model.getKeyWord() + "%");

            TClassExample.Criteria majorCriteria = example.createCriteria();
            majorCriteria.andMajorLike("%" + model.getKeyWord() + "%");

            TClassExample.Criteria nameCriteria = example.createCriteria();
            nameCriteria.andNameLike("%" + model.getKeyWord() + "%");

            example.or(departmentCriteria);
            example.or(majorCriteria);
            example.or(nameCriteria);
        } else {
            TClassExample.Criteria criteria = example.createCriteria();
            criteria.when(StringUtils.isNoneBlank(model.getDepartment()), criteria1 -> criteria.andDepartmentLike("%" + model.getDepartment() + "%"))
                    .when(StringUtils.isNoneBlank(model.getMajor()), criteria1 -> criteria.andMajorLike("%" + model.getMajor() + "%"))
                    .when(StringUtils.isNoneBlank(model.getName()), criteria1 -> criteria.andNameLike("%" + model.getName() + "%"));
        }
        PageHelper.startPage(model.getPageIndex(), model.getPageSize());
        return mapper.selectByExample(example);
    }

    @Transactional
    public void saveOrUpdateBatch(List<TClass> list) {
        mapper.insertOrUpdateBatch(list);
    }

    @Transactional
    public void deleteAll() {
        TClassExample example = new TClassExample();
        mapper.deleteByExample(example);
    }

    @Override
    public boolean isClassNameValid(ClassEditRequestVM model) {
        if (StringUtils.isBlank(model.getName())){
            return false;
        }
        TClassExample example = new TClassExample();
        TClassExample.Criteria criteria = example.createCriteria();
        criteria.when(Objects.nonNull(model.getId()),criteria1 -> criteria.andIdNotEqualTo(model.getId()))
                .andNameEqualTo(model.getName());
        return mapper.countByExample(example) < 1;
    }
}
